

***************************** Table 3 Panel E **********************************

*** OOS Horserace: This is to perform encompassing test of Harvey, Leybourne, and Newbold (1998)

clear

use mydata

rename *, lower

eststo clear

global ssize = _N

gen dm=mofd(date)
format dm %tm
tsset dm

* Taking lags of variables

gen l1mktrf = l1.mktrf
gen l1amihud =l1.amihud
gen l1tover = l1.tover
gen l1hml = l1.hml
gen l1smb = l1.smb
gen lsyy = l1.syy

* define flow variable here 

gen hfvar = l1.hfflow_ma // this is variable for alternative model. The null model uses the variables in the horse race
gen mfvar = l1.active_ma // this is variable for alternative model. The null model uses the variables in the horse race

save oss_temp, replace

*** Macro for Encompassing tests  

matrix OOS_T=J(7,7,.)

local train 120 // insample period

local x = 1
local num = 1

foreach nullvar in btm characteristics syy_ma cmkt vol_12m issmrep crowd { // variables in Panel E of Table 3

gen testvar = l1.`nullvar'

local h = 1 // holding period	

local ctrlvar l1mktrf l1amihud l1tover l1hml l1smb //control variables

matrix OOS_T[1,`x']=`num' 

local period `train'  
scalar define k = $ssize - `period' //- `f' // - 1 is necessary? 
local y=k //the periods left for forcasting

gen yhat_hf = .  // yhat of alternative model. In this case, hfflow_ma 
gen yhat_mf = .  // yhat of alternative model. In this case, active_ma
gen yhat_n = .  // yhat of null model. this is the prediction based on variables that need to be tested for encompassing
gen pmean =. // prevailaing mean of anomaly 

forvalues i=1/`y' {

drop if _n == $ssize & `nullvar' ==.
qui reg hfvar  `ctrlvar' if _n<= `period' +`i' // model to obtain residual flows
qui predict res_hf if  _n<= `period' +`i' , re  // residual hf moving average  

qui reg mfvar  `ctrlvar' if _n<= `period' +`i' // model to obtain residual flows
qui predict res_mf if  _n<= `period' +`i' , re  // residual mf moving average  

qui reg testvar `ctrlvar' if _n<= `period' +`i' // model to obtain the residual predictor variable
qui predict res_test if  _n<= `period' +`i', re  // obtain residal 

qui reg syy res_hf if _n< `period' +`i'  // alternative model: anomalies on HF 
qui replace yhat_hf=_b[_cons] + _b[res_hf]*res_hf  if _n== `period' +`i' 

qui reg syy res_mf if _n< `period' +`i'  // alternative model: anomalies on MF
qui replace yhat_mf=_b[_cons] + _b[res_mf]*res_mf  if _n== `period' +`i' 

qui reg syy res_test if _n< `period' +`i'  // null model: anomalies on encompassing test variable.
qui replace yhat_n=_b[_cons] + _b[res_test]*res_test  if _n== `period' +`i' 

qui su syy if _n< `period' +`i'
qui replace pmean=r(mean) if _n== `period' +`i' // prevailing returns. To calculate OOSR2 of the test variable

drop res_hf res_mf res_test
}

** forecast errors (fe)
gen fe_hf = syy - yhat_hf
gen fe_mf = syy - yhat_mf
gen fe_n = syy - yhat_n

** For HF
gen fe_hfn = fe_n*fe_hf
gen dfehf = fe_n - fe_hf 
gen dthf = dfehf*fe_n
gen dtsqhf = dthf^2

** For MF
gen fe_mfn = fe_n*fe_mf
gen dfemf = fe_n - fe_mf 
gen dtmf = dfemf*fe_n
gen dtsqmf = dtmf^2

*** R2 of test vars (null model) **
*Sqaure of Predicted error (SE)

gen se_n = (syy - yhat_n)^2 // squared prediction error using test var: null model 
gen se_pm = (syy - pmean)^2 // squared prediction error based on prevailing means

su se_n
scalar define MSE_N = r(mean)
su se_pm 
scalar define MSE_pm = r(mean)

*** OOS R2 (test var)

scalar define OOSR2 = (1 - MSE_N/MSE_pm)

dis "OSS-R2:  " OOSR2 

matrix OOS_T[2,`x']= OOSR2*100

** CW OOS-T, bs
gen CW = se_pm - se_n + (pmean - yhat_n)^2

qui reg CW , vce(bootstrap, rep(1000))
matrix OOS_T[3,`x']=_b[_cons]/_se[_cons]

**** encompassing test: 

** lambda HF**
qui reg fe_n dfehf
scalar define lambdahf = _b[dfehf]
di lambdahf
matrix OOS_T[4,`x']= lambdahf

** HLN stats HF**
su dthf
scalar define Mdthf = r(mean)

su dtsqhf
scalar define Qthf = r(sum)/k 

gen dtlaghf = l`h'.dthf 
gen autoadjhf = dthf*dtlaghf 

su autoadjhf
scalar define Qthf_adj = Qthf + 1/k *(1-1/`h')*r(sum)  // autocorrelation adjustment 

scalar defin HLNhf = (sqrt(k)/sqrt(Qthf_adj))*Mdthf
dis HLNhf

matrix OOS_T[5,`x']= HLNhf

** lambda MF **
qui reg fe_n dfemf
scalar define lambdamf = _b[dfemf]
di lambdamf 
matrix OOS_T[6,`x']= lambdamf 

** HLN stats MF**
su dtmf
scalar define Mdtmf = r(mean)

su dtsqmf
scalar define Qtmf = r(sum)/k 

gen dtlagmf = l`h'.dtmf 
gen autoadjmf = dtmf*dtlagmf 

su autoadjmf
scalar define Qtmf_adj = Qtmf + 1/k *(1-1/`h')*r(sum)  // autocorrelation adjustment 

scalar defin HLNmf = (sqrt(k)/sqrt(Qtmf_adj))*Mdtmf
dis HLNmf

matrix OOS_T[7,`x']= HLNmf


local ++x
use oss_temp, clear

local ++num
}

mat rownames OOS_T = FactorTimingVars OOS-R2(%) OOS-T_CWbs lambda-HF HLN-HF lambda-MF HLN-MF 

matrix list OOS_T, format(%9.3f)

putexcel set T3E, sheet(Encompassing) modify

putexcel A1=matrix(OOS_T), names
